package com.example.ordersystem;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.ordersystem.pojo.User;
import com.example.ordersystem.utils.ListDataSave;

import java.util.ArrayList;
import java.util.List;

/*
 * 信息修改页面
 */
public class UserInfoActivity extends AppCompatActivity {

    private Intent intent;
    private TextView user_info_username;
    private EditText user_info_phone;
    private EditText user_info_address;
    private Button btn_user_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_info);

        initView();
    }

    private void initView() {
        user_info_address = findViewById(R.id.user_info_address);
        user_info_phone = findViewById(R.id.user_info_phone);
        user_info_username = findViewById(R.id.user_info_username);
        btn_user_info = findViewById(R.id.btn_user_info);

        User user1 = null;
        final String userLogin = ListDataSave.getUserLogin(this);
        List<User> userList = ListDataSave.getUserList(this, userLogin);
        for (User user : userList) {
            if (userLogin.equals(user.getName())) {
                user1 = user;
                break;
            }
        }
        user_info_username.setText(user1.getName());
        user_info_phone.setText(user1.getPhone());
        user_info_address.setText(user1.getAddress());

        final String password = user1.getPassword();
        btn_user_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String phone = user_info_phone.getText().toString().trim();
                String address = user_info_address.getText().toString().trim();
                List<User> userList = new ArrayList<>();
                userList.add(new User(ListDataSave.getUserLogin(UserInfoActivity.this), password, phone, address));
                ListDataSave.putUserBean(UserInfoActivity.this, userList, ListDataSave.getUserLogin(UserInfoActivity.this));

                Toast.makeText(UserInfoActivity.this, "修改成功", Toast.LENGTH_SHORT).show();

                intent = new Intent(UserInfoActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}
